<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/23
 * Time: 11:35
 */

namespace app\share\model;


use think\Model;
use think\Db;

class PingStore extends Model
{
    protected $table = 'wisdom_ip_store';
    protected $STATUS_TRUE = 1;//已启用
    protected $STATUS_FALSE = 2;//未使用

    public function edit($data,$id)
    {
        $where['ip_id'] = $id;
        $res = $this->where($where)->update($data);
        if($res)
        {
            returnJson(200,  "修改成功")->send();die;
        }else
        {
            returnJson(200,  "数据未修改")->send();die;
        }
    }
    public function deleteIp($id)
    {
        $where['ip_id'] = $id;
        $res = $this->where($where)->delete();
        if($res)
        {
            returnJson(200,  "删除成功")->send();die;
        }else
        {
            returnJson(-1002,  "数据id不存在")->send();die;
        }
    }

    public function isStatus($id,$status)
    {
        if($status==1)
        {
            $is_update = $this->STATUS_FALSE;
        }else
        {
            $is_update = $this->STATUS_TRUE;
        }
        $ping['ip_status'] = $is_update;
        $where['ip_id'] = $id;
        $res = $this->where($where)->update($ping);
        if($res)
        {
            returnJson(200,  "修改成功")->send();die;
        }else
        {
            returnJson(200,  "数据未修改")->send();die;
        }
    }
    public function ipSelect($where,$page,$size)
    {
        $data = $this
                ->field("
                ip_id,
                net_name,
                ip_address,
                ip_server,
                server_name,
                ip_remark,
                ip_update,
                admin_tel,
                name,
                ip_status,
                depart_id,
                wisdom_ip_store.company_id as company_id
                ")
                ->join('server_name','wisdom_ip_store.ip_server = server_name.server_id')
                ->join('admin_user','wisdom_ip_store.ip_user = admin_user.admin_id')
//                ->join('company','wisdom_ip_store.company_id = company.company_id')
                ->where($where)
                ->order(['ip_update'=>'desc'])
                ->page($page, $size)
                ->select();
        foreach($data as $k=>$v)
        {
            $res_company = Db::name("company")->field("company_name")->where(['company_id'=>$v['company_id']])->find();
            $data[$k]['company_name'] = $res_company['company_name'];
            $res_company = Db::name("company")->field("company_name")->where(['company_id'=>$v['depart_id']])->find();
            $data[$k]['depart_name'] = $res_company['company_name'];

        }


        $total=$this
                ->join('server_name','wisdom_ip_store.ip_server = server_name.server_id')
                ->join('admin_user','wisdom_ip_store.ip_user = admin_user.admin_id')
//                ->join('company','wisdom_ip_store.company_id = company.company_id')
                ->where($where)
                ->count();
        $ping['total']  = $total;
        $ping['page']   = $page;
        $ping['size']   = $size;
        $ping['data']   = $data;
        returnJson(200,  "success", $ping)->send();die;
    }
    public function progressStatus($id)
    {
        $data = Db::name("set_pro")->field("pro_sum,pro_num")->where(['user_id'=>$id])->find();
        if($data)
        {
            $speed = $data['pro_num'] / $data['pro_sum'] * 100;
            $speed = explode(".",$speed);
            $speed = $speed['0'];
            returnJson(200,  "success", ['speed'=>$speed])->send();die;
        }else
        {
            returnJson(200,  "success", ['speed'=>100])->send();die;
        }

    }
    public function batchNorMal($page,$size,$id)
    {
        $where['ip_status'] = 3;
        $where['ip_user'] = $id;
        $data= Db::name("batch_ip_log")
            ->field("net_name,ip_address,server_name,ip_remark,ip_note")
            ->join('server_name','wisdom_batch_ip_log.ip_server = server_name.server_id','LEFT')
            ->where($where)
            ->order(['ip_id'=>'desc'])
            ->page($page, $size)
            ->select();
        $total = Db::name("batch_ip_log")
            ->join('server_name','wisdom_batch_ip_log.ip_server = server_name.server_id','LEFT')
            ->where($where)
            ->count();
        $ping['total']  = $total;
        $ping['page']   = $page;
        $ping['size']   = $size;
        $ping['data']   = $data;
        returnJson(200,  "success", $ping)->send();die;

    }
    public function addIpAddress($data)
    {
        //ip唯一性判断
        $where['ip_address'] = $data['ip_address'];
        $only = $this->where($where)->find();
        if($only)
        {
            returnJson(-402,  "ip地址已存在")->send();die;
        }
        //ip格式判断
        if( filter_var( $data['ip_address'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ){        //IPv6
        }elseif( filter_var( $data['ip_address'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ){    //IPv4
        }else{
            returnJson(-403,  "ip地址格式错误")->send();die;
        }
        //添加入库
        $res = $this->insert($data);
        if($res)
        {
            returnJson(200,  "添加成功")->send();die;
        }else
        {
            returnJson(-401,  "添加失败")->send();die;
        }
    }





}